<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTransactionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 创建新的交易表
        Schema::create('transactions', function (Blueprint $table) {
            // 主键 - 雪花ID
            $table->unsignedBigInteger('id')->primary()->comment('交易ID(雪花ID)');
            
            // 关联字段
            $table->unsignedBigInteger('tenant_id')->comment('租户ID');
            
            // 基础交易信息
            $table->string('transaction_number', 50)->comment('账单ID');
            $table->enum('type', ['income', 'expense'])->comment('交易类型：入账、出账');
            $table->string('sub_type', 50)->comment('子类型');
            $table->decimal('amount', 15, 2)->comment('交易金额');
            $table->decimal('balance_after', 15, 2)->comment('交易后余额');
            $table->string('landing_company', 200)->comment('交易落地公司');
            
            // 状态管理
            $table->enum('status', [
                'pending',      // 待处理
                'processing',   // 处理中
                'completed',    // 已完成
                'failed',       // 失败
                'cancelled'     // 已取消
            ])->default('pending')->comment('交易状态');
            
            // 交易描述和摘要
            $table->text('summary')->nullable()->comment('交易摘要');
            $table->text('description')->nullable()->comment('详细描述');
            
            // 额外数据
            $table->json('metadata')->nullable()->comment('额外数据');
            
            // 时间戳字段
            $table->timestamp('transaction_time')->comment('交易时间');
            $table->timestamp('created_at')->nullable()->comment('创建时间');
            $table->timestamp('updated_at')->nullable()->comment('更新时间');
            $table->timestamp('processed_at')->nullable()->comment('处理时间');
            
            // 操作人员
            $table->unsignedBigInteger('created_by')->nullable()->comment('创建人ID');
            $table->unsignedBigInteger('processed_by')->nullable()->comment('处理人ID');
            
            // 外键约束
            $table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
            
            // 索引
            $table->index(['tenant_id', 'type'], 'idx_tenant_type');
            $table->index(['tenant_id', 'sub_type'], 'idx_tenant_sub_type');
            $table->index(['tenant_id', 'status'], 'idx_tenant_status');
            $table->index(['tenant_id', 'transaction_time'], 'idx_tenant_transaction_time');
            $table->index(['tenant_id', 'created_at'], 'idx_tenant_created_at');
            $table->index(['transaction_number'], 'idx_transaction_number');
            
            // 租户级唯一约束
            $table->unique(['tenant_id', 'transaction_number'], 'uk_tenant_transaction_number');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('transactions');
    }
};